<template>
  <div>
    <el-row>
      <el-table
          fixed
          :data="tableData"
          style="width: 100%">
        <el-table-column
            fixed
            type="index"
            label="序号"
            width="140">
        </el-table-column>
        <el-table-column
            prop="categoryName"
            label="类别名称"
            width="360">
        </el-table-column>
        <el-table-column
            prop="categoryState"
            label="图书状态"
            width="400">
        </el-table-column>

        <el-table-column
            prop="categorySort"
            label="排序权重"
            width="360">
        </el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
                size="mini"
                @click="edit(scope.$index)">编辑</el-button>
            <el-button
                size="mini"
                type="danger"
                @click="del(scope.$index)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>

    <el-dialog title="类别信息" :visible.sync="dialogFormVisible">
      <el-form :model="categoryForm">
        <el-form-item label="类别名称" :label-width="formLabelWidth">
          <el-input v-model="categoryForm.categoryName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="类别状态" :label-width="formLabelWidth">
          <el-input v-model="categoryForm.categoryState" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序权重" :label-width="formLabelWidth">
          <el-input v-model="categoryForm.categorySort" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="update">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Category",
  data() {
    return {
      tableData: [],
      dialogFormVisible:false,
      formLabelWidth:'120px',
      categoryForm:{
        categoryName:"",
        categoryState:"",
        categorySort:""
      }
    }
  },
  methods:{
    getAllCategory() {
      this.$axios.get("/category/get").then(res => {
        if(res.data.code == 0) {
          this.tableData = res.data.data
          this.$message.success("获取成功")
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    del(index) {
      this.$confirm('确认删除该类别吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios.post("/category/delete",this.tableData[index]).then(res => {
          if(res.data.code == 0) {
            this.$message.success("删除成功")
            location.reload()
          } else {
            this.$message.error(res.data.message)
          }
        })
      }).catch();

    },
    edit(index) {
      this.categoryForm = this.tableData[index]
      this.dialogFormVisible = true
    },
    update() {
      this.$axios.post("/category/update",this.categoryForm).then(res => {
        if(res.data.code == 0) {
          this.$message.success("修改成功")
          location.reload()
        } else {
          this.$message.error(res.data.message)
        }
      })
    }
  },
  created() {
    this.getAllCategory()
  }
}
</script>

<style scoped>

</style>